package com.flying.carclub.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.utils.SPUtils;
import com.stonesun.newssdk.NewsAgent;

public class SettingActivity extends BaseActivity implements View.OnClickListener {
    private View exitBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
    }

    private void initView() {
        exitBtn =  findViewById(R.id.tv_exit);
        findViewById(R.id.bar_about).setOnClickListener(this);
        findViewById(R.id.bar_clear).setOnClickListener(this);
        exitBtn.setOnClickListener(this);
        if (SPUtils.getUserInfo(SettingActivity.this) != null){
            exitBtn.setVisibility(View.VISIBLE);
        }else {
            exitBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        int viewId = v.getId();
        switch (viewId) {
            case R.id.bar_clear:
                showClearDialog();
                break;
            case R.id.bar_about:
                startActivity(new Intent(SettingActivity.this, AboutActivity.class));
                break;
            case R.id.tv_exit:
                SPUtils.clearUserInfo(SettingActivity.this);
                NewsAgent.logOutComment();
                setResult(RESULT_OK);
                SettingActivity.this.finish();
                break;
            default:
                break;
        }
    }
    private void showClearDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setMessage("是否清除缓存和用户数据")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToast("缓存清楚成功");
                        SPUtils.clearSP(SettingActivity.this);
                        NewsAgent.logOutComment();
                        setResult(RESULT_OK);
                        SettingActivity.this.finish();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.show();
    }
}
